import React from "react"
import appImg from "../ToolImg/appImg.png"
import '../style/common.css'
import WebApp from '@twa-dev/sdk';
import { get, postFormData } from "../network/request"
import { Buffer } from "buffer";
import {Address} from '@ton/core';

class WelcomPage extends React.Component{
    constructor(props) {
        super(props)
    }

    componentDidMount() {
        // const buf = Buffer.from('Hello World', 'utf8');
        // console.log("--------------xxxx------------"); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
        // console.log(buf); // 48656c6c6f20576f726c64

        // const address1 = Address.parse('EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPrHF');
        // const address2 = Address.parse('0:3055adc31dce30773ca23697a17acf92711adc0638d06a9853a936422fc1a6fd');

        // // toStrings arguments: urlSafe, bounceable, testOnly
        // // defaults values: true, true, false

        // console.log(address1.toString()); // EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPrHF
        // console.log(address1.toRawString()); // 0:ca6e321c7cce9ecedf0a8ca2492ec8592494aa5fb5ce0387dff96ef6af982a3e

        // console.log(address2.toString()); // EQDKbjIcfM6ezt8KjKJJLshZJJSqX7XOA4ff-W72r5gqPrHF
        // console.log(address2.toRawString()); // 0:ca6e321c7cce9ecedf0a8ca2492ec8592494aa5fb5ce0387dff96ef6af982a3e

        // 组件挂载后设置定时器
        this.requestFetchUserTest();
    
    }


    requestFetchUserTest = () => {
        const formData = new FormData();
        var userId = "3213213213"
        formData.append("userId", userId);

        postFormData("/fetchUser", formData)
            .then(response => {
                if (response && response["success"] == true && response.data) {
                    localStorage.setItem("initUser", JSON.stringify(response.data));
                }
            })
            .catch(error => {
                console.error("数据提交失败:", error);
                this.setState({userName: "失败" + error})
            });
    }


    requestFetchUser = () => {
        const formData = new FormData();
        const initData = WebApp.initDataUnsafe;
        var userId = initData.user.id
        
        formData.append("userId", userId);

        if(initData.user.first_name){
            formData.append("firstName", initData.user.first_name);
        }else{
            formData.append("firstName", "nihao");
        }
        if(initData.user.last_name){
            formData.append("lastName", initData.user.last_name);
        }else{
            formData.append("lastName", "nihao");
        }
        if(initData.user.username){
            formData.append("userName", initData.user.username);
        }else{
            formData.append("userName", "nihao");
        }
        
        if(initData.user.photo_url){
            formData.append("photoUrl", initData.user.photo_url);
        }else{
            formData.append("photoUrl", "nihao");
        }
        if(initData.user.language_code){
            formData.append("languageCode", initData.user.language_code);
        }else{
            formData.append("languageCode", "nihao");
        }

        if(initData.user.is_bot){
            formData.append("isBot", initData.user.is_bot);
        }

        postFormData("/fetchUser", formData)
            .then(response => {
                if (response && response["success"] == true && response.data) {
                    localStorage.setItem("initUser", JSON.stringify(response.data));
                }
            })
            .catch(error => {
                console.error("数据提交失败:", error);
            });
    }

    render(){
        return(
            <div className="welcomeBGV">
                <div className="welcomeContentV">
                    <div className="welcomeV">
                        <img src={appImg} className="appImgV"></img>
                        <p className="welcomeL">TechHub</p>
                    </div>
                </div>
                <div className="startBtn" onClick={this.props.onClick}>
                    <p className="startL">Start</p>
                </div>
            </div>
        )
    }
}

export default WelcomPage
